package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageAllocatingMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageAllocatingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageAllocatingOverviewBean;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * 作者：wsy on 2017/12/2 09:31
 * <p>
 * 邮箱：445979770@qq.com
 */

public class AllocatingInStorageItemListAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageAllocatingOverviewBean.ReceiveInAllocatingStorageListBean.StockInputItemBean> mAllocatingInStorageItemList;
    private LayoutInflater inflater;
    private InStorageAllocatingOverviewActivity.InStorageAllocatingOverviewListener mInStorageAllocatingListener;
    private String mMaterielCode, mTransferNumber, mDeliveryNumber, mApplyDepartment, mApplyPerson, mRequirementsDate, mReceivingFactory, mReceivingLocation, mDeliveryDate , mDeliveryVehicles , mDeliveryDriver , mDeliveryNote , isShow;

    public AllocatingInStorageItemListAdapter(Context context, InStorageAllocatingOverviewBean.ReceiveInAllocatingStorageListBean data, List<InStorageAllocatingOverviewBean.ReceiveInAllocatingStorageListBean.StockInputItemBean> inStorageAllocatingItemList, String materielCode, InStorageAllocatingOverviewActivity.InStorageAllocatingOverviewListener inStorageReceiveOverviewListener) {
        mContext = context;
        mTransferNumber = data.getAllocate_code();
        mDeliveryNumber = data.getAllocate_delivery_code();
        mApplyDepartment = data.getOrg_name();
        mApplyPerson = data.getApplicant();
        mRequirementsDate = data.getDemand_date();
        mReceivingFactory = data.getFty_input_code() + "-" + data.getFty_input_name();
        mReceivingLocation = data.getLocation_input_code() + " - " + data.getLocation_input_name();
        isShow = data.getDelivery_info().getIs_show();
        if (isShow.equals("0")) {
            mDeliveryDate = data.getDelivery_info().getDelivery_date();
            mDeliveryVehicles = data.getDelivery_info().getDelivery_vehicle();
            mDeliveryDriver = data.getDelivery_info().getDelivery_driver();
            mDeliveryNote = data.getDelivery_info().getRemark();
        }
        mAllocatingInStorageItemList = inStorageAllocatingItemList;
        mInStorageAllocatingListener = inStorageReceiveOverviewListener;
        mMaterielCode = materielCode;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mAllocatingInStorageItemList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mAllocatingInStorageItemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_allocating_item_list_first_item, null);
            TextView transferNumber = (TextView) convertView.findViewById(R.id.transferNumber);
            TextView deliveryNumber = (TextView) convertView.findViewById(R.id.deliveryNumber);
            TextView applyDepartment = (TextView) convertView.findViewById(R.id.applyDepartment);
            TextView applyPerson = (TextView) convertView.findViewById(R.id.applyPerson);
            TextView requirementsDate = (TextView) convertView.findViewById(R.id.requirementsDate);
            TextView receivingFactory = (TextView) convertView.findViewById(R.id.receivingFactory);
            TextView receivingLocation = (TextView) convertView.findViewById(R.id.receivingLocation);
            TextView deliveryDate = (TextView) convertView.findViewById(R.id.deliveryDate);
            TextView deliveryVehicles = (TextView) convertView.findViewById(R.id.deliveryVehicles);
            TextView deliveryDriver = (TextView) convertView.findViewById(R.id.deliveryDriver);
            TextView deliveryNote = (TextView) convertView.findViewById(R.id.deliveryNote);
            TextView showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
            PercentRelativeLayout hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
            PercentRelativeLayout hideLayout1 = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout1);
            EditText materielCode = (EditText) convertView.findViewById(R.id.materielCode);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            transferNumber.setText(mTransferNumber);
            applyDepartment.setText(mApplyDepartment);
            deliveryNumber.setText(mDeliveryNumber);
            applyPerson.setText(mApplyPerson);
            requirementsDate.setText(mRequirementsDate);
            receivingFactory.setText(mReceivingFactory);
            receivingLocation.setText(mReceivingLocation);
            materielCode.setText(mMaterielCode);
            if (isShow.equals("0")) {
                deliveryDate.setText(mDeliveryDate);
                deliveryVehicles.setText(mDeliveryVehicles);
                deliveryDriver.setText(mDeliveryDriver);
                deliveryNote.setText(mDeliveryNote);
            }
            showAndHide.setOnClickListener((View v) -> {
                if (hideLayout.getVisibility() == View.GONE) {
                    hideLayout.setVisibility(View.VISIBLE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_hide_information));
                } else if (hideLayout.getVisibility() == View.VISIBLE) {
                    hideLayout.setVisibility(View.GONE);
                    ((TextView) v).setText(mContext.getResources().getString(R.string.activity_receive_delivery_overview_show_information));
                }
            });
//            materielCode.addTextChangedListener(new QRCodeEditTextWatcher(materielCode) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, materielCode);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mInStorageAllocatingListener.onNoDoubleClickListener(materielCode.getText().toString());
                }
            });
        } else {
            InStorageAllocatingOverviewBean.ReceiveInAllocatingStorageListBean.StockInputItemBean receiveInstorageItem = mAllocatingInStorageItemList.get(position - 1);
            AllocatingInStorageItemListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_allocating_item_list, null);
                viewHolder = new AllocatingInStorageItemListAdapter.ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (AllocatingInStorageItemListAdapter.ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveInstorageItem);
        }
        return convertView;
    }

    private void initView(AllocatingInStorageItemListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.serialNumber = (TextView) view.findViewById(R.id.serialNumber);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescribe = (TextView) view.findViewById(R.id.materielDescribe);
        viewHolder.inStorageNumber = (TextView) view.findViewById(R.id.inStorageNumber);
//        viewHolder.inventory = (TextView) view.findViewById(R.id.inventory);
        viewHolder.batch = (TextView) view.findViewById(R.id.batch);
//        viewHolder.unit = (TextView) view.findViewById(R.id.unit);
//        viewHolder.inStorageNumberMax = (TextView) view.findViewById(R.id.inStorageNumberMax);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
        viewHolder.sendFatory = (TextView) view.findViewById(R.id.sendFatory);
        viewHolder.sendLocation = (TextView) view.findViewById(R.id.sendLocation);
    }

    private void initData(Context context, AllocatingInStorageItemListAdapter.ViewHolder viewHolder, View view, InStorageAllocatingOverviewBean.ReceiveInAllocatingStorageListBean.StockInputItemBean allocatingInStorageItem) {
        viewHolder.serialNumber.setText(allocatingInStorageItem.getStock_input_rid());
        viewHolder.materielCode.setText(allocatingInStorageItem.getMat_code());
        viewHolder.materielDescribe.setText(allocatingInStorageItem.getMat_name());
        viewHolder.inStorageNumber.setText(allocatingInStorageItem.getQty() + allocatingInStorageItem.getName_zh());
//        viewHolder.unit.setText(allocatingInStorageItem.getUnit());
//        viewHolder.inStorageNumberMax.setText(allocatingInStorageItem.getInStorageNumber());
//        viewHolder.inventory.setText(allocatingInStorageItem.getInventoryAddressCode() + " - " + allocatingInStorageItem.getInventoryAddressValue());
        viewHolder.sendFatory.setText(allocatingInStorageItem.getFty_output_code() + "-" + allocatingInStorageItem.getFty_output_name());
        viewHolder.sendLocation.setText(allocatingInStorageItem.getLocation_output_code() + "-" + allocatingInStorageItem.getLocation_output_name());
        boolean flag = InStorageUtils.hasRAllocatingBatchRequired(allocatingInStorageItem.getBatch_material_spec_list(),allocatingInStorageItem.getBatch_spec_list());
        if (flag) {
            String batchInfo = view.getResources().getString(R.string.adapter_in_storage_item_list_batch_required);
            SpannableStringBuilder builder = new SpannableStringBuilder(batchInfo);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
            builder.setSpan(redSpan, batchInfo.length() - 1, batchInfo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            viewHolder.batch.setText(builder);
        } else {
            viewHolder.batch.setText(view.getResources().getString(R.string.adapter_in_storage_item_list_batch_not_required));
        }
//        int flag = InStorageUtils.hasReceiveBatchInfo(receiveInStorageItem.getBatch());
//        switch (flag) {
//            case InStorageUtils.NO_BATCH:
//                viewHolder.batchLayout.setVisibility(View.GONE);
//                break;
//            case InStorageUtils.BATCH_NO_MESSAGE:
//                break;
//            case InStorageUtils.BATCH_HAS_MESSAGE:
//                viewHolder.batchLayout.setVisibility(View.VISIBLE);
//                viewHolder.batch.setText("修改");
//                break;
//            case InStorageUtils.BATCH_NO_MESSAGE_BUT_REQUIRED:
//                viewHolder.batchLayout.setVisibility(View.VISIBLE);
//                viewHolder.batch.setText("必填");
//                break;
//        }
        if (InStorageUtils.checkAllocatingItem(allocatingInStorageItem)) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                if (mInStorageAllocatingListener.onCheckData()) {
                    Intent intent = new Intent(context, InStorageAllocatingMaterielDetailActivity.class);
                    //暂时注释
                    String id = allocatingInStorageItem.getAllocate_item_id();
                    intent.putExtra(InStorageExtra.ID, id);
                    context.startActivity(intent);
//                }
            }
        });
//        viewHolder.inventory.setOnClickListener(new NoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View v) {
//                DialogUtils.getReceiveInventoryAddressDialog(context, InStorageUtils.getReceiveInventoryAddressList(context), new DialogUtils.ReceiveInventoryAddressListener() {
//                    @Override
//                    public void onSelect(Dialog dialog, String inventoryAddressValue, String inventoryAddressCode, String factoryValue, String factoryCode) {
//                        viewHolder.inventory.setText(inventoryAddressCode + " - " + inventoryAddressValue);
//                    }
//                }).show();
//            }
//        });
    }

    private class ViewHolder {
        private TextView serialNumber, materielCode, materielDescribe, batch, inStorageNumber , sendFatory , sendLocation;

//        private EditText inStorageNumber;

        private PercentRelativeLayout headLayout;
    }
}
